package orbitPrototype.objects;

import orbitPrototype.loaders.XMLLoader;

/**
 * Description:	Generates a collection of sprites filling the area specified by width/height.
 * 				The sprites at the right and lower edge gets cropped if needed.
 * 
 * @author Drapan
 *
 */
public class TiledSprite extends DisplayObjectContainer
{
	private String _spriteType = "";
	private String _spriteUniqueName = "";
	
	public TiledSprite(int x, int y, int width, int height, String spriteType, String spriteUniqueName)
	{
		super(width, height);
		
		this.setX(x);
		this.setY(y);
		
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;
		
		populate();
	}
	
	private void populate()
	{
		int tileWidth = Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + _spriteType, _spriteUniqueName)));
		int tileHeight = Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + _spriteType, _spriteUniqueName)));
		
		int tilesX = (this.getWidth() / tileWidth) + 1;
		int tilesY = (this.getHeight() / tileHeight) + 1;
		
		int ySpaceLeft = this.getHeight();
				
		for(int y = 0; y < tilesY; y++)
		{
			int currentTileWidth = tileWidth;
			int currentTileHeight = tileHeight;
			
			int xSpaceLeft = this.getWidth();
			
			for(int x = 0; x < tilesX; x++)
			{
				if(xSpaceLeft < tileWidth)
				{
					currentTileWidth = xSpaceLeft;
				}
				if(ySpaceLeft < tileHeight)
				{
					currentTileHeight = ySpaceLeft;
				}
				
				this.addChild(new Sprite(x * tileWidth, y * tileHeight, currentTileWidth, currentTileHeight, false, _spriteType, _spriteUniqueName));
				
				xSpaceLeft -= currentTileWidth;
			}
			
			ySpaceLeft -= currentTileHeight;
		}
	}
}
